package bgg4j;

import org.w3c.dom.Element;

import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamResult;
import java.io.StringWriter;

public class BggResponse {
    protected final Element elem;
    protected Bgg bgg;
    public BggResponse(Element elem, Bgg bgg) {
        this.elem = elem;
        this.bgg = bgg;
    }
    protected void ensureRootNodeNameIs(String rootName) throws BggException{
        if(!rootName.equals(elem.getNodeName())){
            throw new BggException("Unexpected root node name:"+elem.getNodeName()+". Expected:"+rootName+". Check BGG service availability.\n"+toString(elem));
        }
    }
    protected void ensureRootNodeNameIs(String[] rootNames) throws BggException{
        String actualRootName = elem.getNodeName();
        for (String rootName : rootNames) {
            if (rootName.equals(actualRootName)) {
                return;
            }
        }
        String expected = "";
        for (int i = 0; i < rootNames.length; i++) {
            if (i != 0) {
                expected += " or ";
            }
            expected += rootNames[i];
        }
        throw new BggException("Unexpected root node name:" + elem.getNodeName() + ". Expected:" + expected + ". Check BGG service availability.\n" + toString(elem));
    }
    protected static void ensureRootNodeNameIs(String rootName,Element elem) throws BggException{
        if(!rootName.equals(elem.getNodeName())){
            throw new BggException("Unexpected root node name:"+elem.getNodeName()+". Expected:"+rootName+". Check BGG service availability.\n"+toString(elem));
        }
    }

    private static String toString(Element doc) {
        try{
        StringWriter output = new StringWriter();
        TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), new StreamResult(output));
        return output.toString();
        }catch(TransformerException tfe){
            return "";
        }
    }

    protected String getChildText(String str) {
    	if (elem.getElementsByTagName(str).getLength() > 0){
    	return elem.getElementsByTagName(str).item(0).getTextContent();
    	}else{
    	return null;
    	}
    }
    
    protected String getChildText(String str, boolean all) {
    	if (true == all){
	    	StringBuffer ret = new StringBuffer();
	    	for (int i = 0; i < elem.getElementsByTagName(str).getLength(); i++){
	    		ret.append(elem.getElementsByTagName(str).item(i).getTextContent().replace('\n', ' ').trim());
	    		if (i + 1 < elem.getElementsByTagName(str).getLength()){
	    			ret.append("/");
	    		}
	    	}
	        return ret.toString();
    	}else{
    		return elem.getElementsByTagName(str).item(0).getTextContent();
    	}
    }

    protected int getChildInt(String str) {
        return Integer.valueOf(elem.getElementsByTagName(str).item(0).
                               getTextContent());
    }

    protected String getAttributeText(String str){
    	return elem.getAttribute(str);
    }
    
    @Override public int hashCode() {
        return elem.hashCode();
    }

    @Override public boolean equals(Object obj) {
        if (null == obj) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        if (obj instanceof BggResponse) {
            ( (BggResponse) obj).elem.equals(this.elem);
        }
        return false;
    }
}
